package com.lry.web.fiction.processor;

import com.lry.web.fiction.entity.FictionVariable;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.ArrayList;

import java.util.List;
import java.util.Map;

public class FictionVerifyProcessor implements PageProcessor {

    public FictionVariable fictionVariable;

    public  Map<String,Object> map;
    public FictionVerifyProcessor() {
    }

    public FictionVerifyProcessor(FictionVariable fictionVariable, Map<String,Object> map) {
        this.fictionVariable = fictionVariable;
        this.map = map;
    }

    private Site site = Site.me()
            .setSleepTime(1)//采集间隔时间
            .setTimeOut(1000 * 10)//超时时间
            .setRetrySleepTime(3000)//重试时间
            .setRetryTimes(3);//重试次数

    public Site getSite() {
        return site;
    }

    //解析页面
    public void process(Page page) {
        try{
        String url = page.getUrl().toString();
        String[] fictiontitlelss = fictionVariable.getFictionTitle().split("-!-");
        List<String> fictiontitlee = page.getHtml().xpath(fictiontitlelss[0]).regex(fictiontitlelss.length !=1 ? fictiontitlelss[1] : ".+").all();
        String[] titless = fictionVariable.getTitle().split("-!-");
        List<String> titlee = page.getHtml().xpath(titless[0]).regex(titless.length !=1 ? titless[1] : ".+").all();
        String[] contentss = fictionVariable.getContent().split("-!-");
        List<String> contentt = page.getHtml().xpath(contentss[0]).regex(contentss.length !=1 ? contentss[1] : ".+").all();
        //小说搜索
        if (fictiontitlee.size()!=0) {
            String[] fictionUrls = fictionVariable.getFictionUrl().split("-!-");
            String[] fictiontitlels = fictionVariable.getFictionTitle().split("-!-");
            List<String> fictiontitle = page.getHtml().xpath(fictiontitlels[0]).regex(fictiontitlels.length !=1 ? fictiontitlels[1] : ".+").all();
            List<String> fictionUrl = page.getHtml().xpath(fictionUrls[0]).regex(fictionUrls.length !=1 ? fictionUrls[1] : ".+").all();
            //通过小说标签判断搜索内容是否存在
            if (fictionUrl.size() != 0) {
                String[] pageUrls = fictionVariable.getPage().split("-!-");
                String pageUrl = page.getHtml().xpath(pageUrls[0]).regex(pageUrls.length !=1 ? pageUrls[1] : ".+").get();
                if (!fictionUrl.get(0).contains("http")) {
                    List<String> fictionUrll = new ArrayList<>();
                    fictionUrl.forEach(str -> {
                        fictionUrll.add(fictionVariable.getProtocol() + "://" + fictionVariable.getUrl() + str);
                    });
                    fictionUrl = fictionUrll;
                }
                String fUrl = fictionUrl.get(0);
                if(pageUrl!=null) {
                    if (pageUrl.contains("http")) {
                    } else if (pageUrl.contains("/")) {
                        pageUrl = fictionVariable.getProtocol() + "://" + fictionVariable.getUrl() + pageUrl;
                    } else if (!pageUrl.contains("javascript")) {
                        pageUrl = url + pageUrl;
                    }
                }
                map.put("fictionTitle",fictiontitle.get(0));
                map.put("fictionUrl",fictionUrl.get(0));
                map.put("pageUrl",pageUrl);
                page.addTargetRequest(fUrl);
            }

        }else if(titlee.size()!=0){
            //获取小说信息：title，desc,author,fictionType,cover,FictionUrl,FictionId
            String[] titles = fictionVariable.getTitle().split("-!-");
            String[] descs = fictionVariable.getDescs().split("-!-");
            String[] authors = fictionVariable.getAuthor().split("-!-");
            String[] fictionTypes = fictionVariable.getFictionType().split("-!-");
            String[] covers = fictionVariable.getCover().split("-!-");
            String[] chapterTitles = fictionVariable.getChapterTitle().split("-!-");
            String[] chapterUrls = fictionVariable.getChapterUrl().split("-!-");
            String[] updateTimes = fictionVariable.getUpdateTime().split("-!-");
            List<String> title = page.getHtml().xpath(titles[0]).regex(titles.length !=1 ? titles[1] : ".+").all();
            List<String> desc = page.getHtml().xpath(descs[0]).regex(descs.length !=1 ? descs[1] : ".+").all();
            List<String> author = page.getHtml().xpath(authors[0]).regex(authors.length !=1 ? authors[1] : ".+").all();
            List<String> fictionType = page.getHtml().xpath(fictionTypes[0]).regex(fictionTypes.length !=1 ? fictionTypes[1] : ".+").all();
            List<String> cover = page.getHtml().xpath(covers[0]).regex(covers.length !=1 ? covers[1] : ".+").all();
            List<String> chapterTitle = page.getHtml().xpath(chapterTitles[0]).regex(chapterTitles.length !=1 ? chapterTitles[1] : ".+").all();
            List<String> chapterUrl = page.getHtml().xpath(chapterUrls[0]).regex(chapterUrls.length !=1 ? chapterUrls[1] : ".+").all();
            List<String> updateTime = page.getHtml().xpath(updateTimes[0]).regex(updateTimes.length !=1 ? updateTimes[1] : ".+").all();
            if(chapterTitles.length ==3){
                String[] splitTitl = chapterTitles[2].split(",");
                if(splitTitl.length ==1){
                    chapterTitle = chapterTitle.subList(Integer.parseInt(splitTitl[0]),chapterTitle.size());
                }else{
                    chapterTitle = chapterTitle.subList(Integer.parseInt(splitTitl[0]),Integer.parseInt(splitTitl[1]));

                }
            }
            if(chapterUrls.length == 3){
                String[] splitUrls = chapterUrls[2].split(",");
                if(splitUrls.length ==1){
                    chapterUrl = chapterUrl.subList(Integer.parseInt(splitUrls[0]),chapterUrl.size());
                }else{
                    chapterUrl = chapterUrl.subList(Integer.parseInt(splitUrls[0]),Integer.parseInt(splitUrls[1]));

                }

            }



            //判断是否存在
            if (title.size() == 0) {
                title.add(" ");
            }
            if (author.size() == 0) {
                author.add(" ");
            }
            if (fictionType.size() == 0) {
                fictionType.add(" ");
            }
            if (cover.size() == 0) {
                cover.add(" ");
            }
            if (updateTime.size() == 0) {
                updateTime.add(" ");
            }
            if (desc.size() == 0) {
                desc.add(" ");
            }
            if (chapterTitle.size() == 0) {
                chapterTitle.add(" ");
            }
            if (chapterUrl.size() == 0) {
                chapterUrl.add(" ");
            }
            if(!cover.get(0).contains("http")){
                cover.set(0,fictionVariable.getProtocol() + "://" + fictionVariable.getUrl() + cover.get(0));
            }
            String[] st =  chapterUrl.get(0).split("/");
            String chapterId = (st[st.length-1].replace(".html",""));
            String fictionId = st[st.length-2].replace(".html","");
            map.put("title",title.get(0));
            map.put("fictionType",fictionType.get(0));
            map.put("author",author.get(0));
            map.put("descs",desc.get(0));
            map.put("cover",cover.get(0));
            map.put("updateTime",updateTime.get(0));
            map.put("chapterTitle",chapterTitle.get(0));
            map.put("chapterUrl",chapterUrl.get(0));
            map.put("chapterId",chapterId);
            map.put("fictionId",fictionId);
            String fUrl = chapterUrl.get(0);
            if(!chapterUrl.get(0).contains("http")){
                fUrl = fictionVariable.getProtocol()+"://"+fictionVariable.getUrl()+fUrl;
            }
            page.addTargetRequest(fUrl);

        }else if(contentt.size()!=0){
            String[] contents = fictionVariable.getContent().split("-!-");
            List<String> content = page.getHtml().xpath(contents[0]).regex(contents.length !=1 ? contents[1] : ".+").all();
            String[] listUrl =  page.getUrl().toString().split("/");
            map.put("content",content);
        }
    }catch (Exception e) {
            System.out.println(e);
            map.put("Exception:",e.toString());
        }
    }
}

